home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / xml / dom / pulldom.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  12KB  |  431 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import xml.sax as xml
  5. import xml.sax.handler as xml
  6. import types
  7.  
  8. try:
  9.     _StringTypes = [
  10.         types.StringType,
  11.         types.UnicodeType]
  12. except AttributeError:
  13.     _StringTypes = [
  14.         types.StringType]
  15.  
  16. START_ELEMENT = 'START_ELEMENT'
  17. END_ELEMENT = 'END_ELEMENT'
  18. COMMENT = 'COMMENT'
  19. START_DOCUMENT = 'START_DOCUMENT'
  20. END_DOCUMENT = 'END_DOCUMENT'
  21. PROCESSING_INSTRUCTION = 'PROCESSING_INSTRUCTION'
  22. IGNORABLE_WHITESPACE = 'IGNORABLE_WHITESPACE'
  23. CHARACTERS = 'CHARACTERS'
  24.  
  25. class PullDOM(xml.sax.ContentHandler):
  26.     _locator = None
  27.     document = None
  28.     
  29.     def __init__(self, documentFactory = None):
  30.         XML_NAMESPACE = XML_NAMESPACE
  31.         import xml.dom
  32.         self.documentFactory = documentFactory
  33.         self.firstEvent = [
  34.             None,
  35.             None]
  36.         self.lastEvent = self.firstEvent
  37.         self.elementStack = []
  38.         self.push = self.elementStack.append
  39.         
  40.         try:
  41.             self.pop = self.elementStack.pop
  42.         except AttributeError:
  43.             pass
  44.  
  45.         self._ns_contexts = [
  46.             {
  47.                 XML_NAMESPACE: 'xml' }]
  48.         self._current_context = self._ns_contexts[-1]
  49.         self.pending_events = []
  50.  
  51.     
  52.     def pop(self):
  53.         result = self.elementStack[-1]
  54.         del self.elementStack[-1]
  55.         return result
  56.  
  57.     
  58.     def setDocumentLocator(self, locator):
  59.         self._locator = locator
  60.  
  61.     
  62.     def startPrefixMapping(self, prefix, uri):
  63.         if not hasattr(self, '_xmlns_attrs'):
  64.             self._xmlns_attrs = []
  65.         
  66.         if not prefix:
  67.             pass
  68.         self._xmlns_attrs.append(('xmlns', uri))
  69.         self._ns_contexts.append(self._current_context.copy())
  70.         if not prefix:
  71.             pass
  72.         self._current_context[uri] = None
  73.  
  74.     
  75.     def endPrefixMapping(self, prefix):
  76.         self._current_context = self._ns_contexts.pop()
  77.  
  78.     
  79.     def startElementNS(self, name, tagName, attrs):
  80.         xmlns_uri = 'http://www.w3.org/2000/xmlns/'
  81.         xmlns_attrs = getattr(self, '_xmlns_attrs', None)
  82.         if xmlns_attrs is not None:
  83.             for aname, value in xmlns_attrs:
  84.                 attrs._attrs[(xmlns_uri, aname)] = value
  85.             
  86.             self._xmlns_attrs = []
  87.         
  88.         (uri, localname) = name
  89.         if uri:
  90.             if tagName is None:
  91.                 prefix = self._current_context[uri]
  92.                 if prefix:
  93.                     tagName = prefix + ':' + localname
  94.                 else:
  95.                     tagName = localname
  96.             
  97.             if self.document:
  98.                 node = self.document.createElementNS(uri, tagName)
  99.             else:
  100.                 node = self.buildDocument(uri, tagName)
  101.         elif self.document:
  102.             node = self.document.createElement(localname)
  103.         else:
  104.             node = self.buildDocument(None, localname)
  105.         for aname, value in attrs.items():
  106.             (a_uri, a_localname) = aname
  107.             if a_uri == xmlns_uri:
  108.                 if a_localname == 'xmlns':
  109.                     qname = a_localname
  110.                 else:
  111.                     qname = 'xmlns:' + a_localname
  112.                 attr = self.document.createAttributeNS(a_uri, qname)
  113.                 node.setAttributeNodeNS(attr)
  114.             elif a_uri:
  115.                 prefix = self._current_context[a_uri]
  116.                 if prefix:
  117.                     qname = prefix + ':' + a_localname
  118.                 else:
  119.                     qname = a_localname
  120.                 attr = self.document.createAttributeNS(a_uri, qname)
  121.                 node.setAttributeNodeNS(attr)
  122.             else:
  123.                 attr = self.document.createAttribute(a_localname)
  124.                 node.setAttributeNode(attr)
  125.             attr.value = value
  126.         
  127.         self.lastEvent[1] = [
  128.             (START_ELEMENT, node),
  129.             None]
  130.         self.lastEvent = self.lastEvent[1]
  131.         self.push(node)
  132.  
  133.     
  134.     def endElementNS(self, name, tagName):
  135.         self.lastEvent[1] = [
  136.             (END_ELEMENT, self.pop()),
  137.             None]
  138.         self.lastEvent = self.lastEvent[1]
  139.  
  140.     
  141.     def startElement(self, name, attrs):
  142.         if self.document:
  143.             node = self.document.createElement(name)
  144.         else:
  145.             node = self.buildDocument(None, name)
  146.         for aname, value in attrs.items():
  147.             attr = self.document.createAttribute(aname)
  148.             attr.value = value
  149.             node.setAttributeNode(attr)
  150.         
  151.         self.lastEvent[1] = [
  152.             (START_ELEMENT, node),
  153.             None]
  154.         self.lastEvent = self.lastEvent[1]
  155.         self.push(node)
  156.  
  157.     
  158.     def endElement(self, name):
  159.         self.lastEvent[1] = [
  160.             (END_ELEMENT, self.pop()),
  161.             None]
  162.         self.lastEvent = self.lastEvent[1]
  163.  
  164.     
  165.     def comment(self, s):
  166.         if self.document:
  167.             node = self.document.createComment(s)
  168.             self.lastEvent[1] = [
  169.                 (COMMENT, node),
  170.                 None]
  171.             self.lastEvent = self.lastEvent[1]
  172.         else:
  173.             event = [
  174.                 (COMMENT, s),
  175.                 None]
  176.             self.pending_events.append(event)
  177.  
  178.     
  179.     def processingInstruction(self, target, data):
  180.         if self.document:
  181.             node = self.document.createProcessingInstruction(target, data)
  182.             self.lastEvent[1] = [
  183.                 (PROCESSING_INSTRUCTION, node),
  184.                 None]
  185.             self.lastEvent = self.lastEvent[1]
  186.         else:
  187.             event = [
  188.                 (PROCESSING_INSTRUCTION, target, data),
  189.                 None]
  190.             self.pending_events.append(event)
  191.  
  192.     
  193.     def ignorableWhitespace(self, chars):
  194.         node = self.document.createTextNode(chars)
  195.         self.lastEvent[1] = [
  196.             (IGNORABLE_WHITESPACE, node),
  197.             None]
  198.         self.lastEvent = self.lastEvent[1]
  199.  
  200.     
  201.     def characters(self, chars):
  202.         node = self.document.createTextNode(chars)
  203.         self.lastEvent[1] = [
  204.             (CHARACTERS, node),
  205.             None]
  206.         self.lastEvent = self.lastEvent[1]
  207.  
  208.     
  209.     def startDocument(self):
  210.         if self.documentFactory is None:
  211.             import xml.dom.minidom as xml
  212.             self.documentFactory = xml.dom.minidom.Document.implementation
  213.         
  214.  
  215.     
  216.     def buildDocument(self, uri, tagname):
  217.         node = self.documentFactory.createDocument(uri, tagname, None)
  218.         self.document = node
  219.         self.lastEvent[1] = [
  220.             (START_DOCUMENT, node),
  221.             None]
  222.         self.lastEvent = self.lastEvent[1]
  223.         self.push(node)
  224.         for e in self.pending_events:
  225.             if e[0][0] == PROCESSING_INSTRUCTION:
  226.                 (_, target, data) = e[0]
  227.                 n = self.document.createProcessingInstruction(target, data)
  228.                 e[0] = (PROCESSING_INSTRUCTION, n)
  229.             elif e[0][0] == COMMENT:
  230.                 n = self.document.createComment(e[0][1])
  231.                 e[0] = (COMMENT, n)
  232.             else:
  233.                 raise AssertionError('Unknown pending event ', e[0][0])
  234.             self.lastEvent[1] = e
  235.             self.lastEvent = e
  236.         
  237.         self.pending_events = None
  238.         return node.firstChild
  239.  
  240.     
  241.     def endDocument(self):
  242.         self.lastEvent[1] = [
  243.             (END_DOCUMENT, self.document),
  244.             None]
  245.         self.pop()
  246.  
  247.     
  248.     def clear(self):
  249.         self.document = None
  250.  
  251.  
  252.  
  253. class ErrorHandler:
  254.     
  255.     def warning(self, exception):
  256.         print exception
  257.  
  258.     
  259.     def error(self, exception):
  260.         raise exception
  261.  
  262.     
  263.     def fatalError(self, exception):
  264.         raise exception
  265.  
  266.  
  267.  
  268. class DOMEventStream:
  269.     
  270.     def __init__(self, stream, parser, bufsize):
  271.         self.stream = stream
  272.         self.parser = parser
  273.         self.bufsize = bufsize
  274.         if not hasattr(self.parser, 'feed'):
  275.             self.getEvent = self._slurp
  276.         
  277.         self.reset()
  278.  
  279.     
  280.     def reset(self):
  281.         self.pulldom = PullDOM()
  282.         self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  283.         self.parser.setContentHandler(self.pulldom)
  284.  
  285.     
  286.     def __getitem__(self, pos):
  287.         rc = self.getEvent()
  288.         if rc:
  289.             return rc
  290.         
  291.         raise IndexError
  292.  
  293.     
  294.     def next(self):
  295.         rc = self.getEvent()
  296.         if rc:
  297.             return rc
  298.         
  299.         raise StopIteration
  300.  
  301.     
  302.     def __iter__(self):
  303.         return self
  304.  
  305.     
  306.     def expandNode(self, node):
  307.         event = self.getEvent()
  308.         parents = [
  309.             node]
  310.         while event:
  311.             (token, cur_node) = event
  312.             if cur_node is node:
  313.                 return None
  314.             
  315.             if token != END_ELEMENT:
  316.                 parents[-1].appendChild(cur_node)
  317.             
  318.             if token == START_ELEMENT:
  319.                 parents.append(cur_node)
  320.             elif token == END_ELEMENT:
  321.                 del parents[-1]
  322.             
  323.             event = self.getEvent()
  324.  
  325.     
  326.     def getEvent(self):
  327.         if not self.pulldom.firstEvent[1]:
  328.             self.pulldom.lastEvent = self.pulldom.firstEvent
  329.         
  330.         while not self.pulldom.firstEvent[1]:
  331.             buf = self.stream.read(self.bufsize)
  332.             if not buf:
  333.                 self.parser.close()
  334.                 return None
  335.             
  336.             self.parser.feed(buf)
  337.         rc = self.pulldom.firstEvent[1][0]
  338.         self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
  339.         return rc
  340.  
  341.     
  342.     def _slurp(self):
  343.         self.parser.parse(self.stream)
  344.         self.getEvent = self._emit
  345.         return self._emit()
  346.  
  347.     
  348.     def _emit(self):
  349.         rc = self.pulldom.firstEvent[1][0]
  350.         self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
  351.         return rc
  352.  
  353.     
  354.     def clear(self):
  355.         self.pulldom.clear()
  356.         del self.pulldom
  357.         self.parser = None
  358.         self.stream = None
  359.  
  360.  
  361.  
  362. class SAX2DOM(PullDOM):
  363.     
  364.     def startElementNS(self, name, tagName, attrs):
  365.         PullDOM.startElementNS(self, name, tagName, attrs)
  366.         curNode = self.elementStack[-1]
  367.         parentNode = self.elementStack[-2]
  368.         parentNode.appendChild(curNode)
  369.  
  370.     
  371.     def startElement(self, name, attrs):
  372.         PullDOM.startElement(self, name, attrs)
  373.         curNode = self.elementStack[-1]
  374.         parentNode = self.elementStack[-2]
  375.         parentNode.appendChild(curNode)
  376.  
  377.     
  378.     def processingInstruction(self, target, data):
  379.         PullDOM.processingInstruction(self, target, data)
  380.         node = self.lastEvent[0][1]
  381.         parentNode = self.elementStack[-1]
  382.         parentNode.appendChild(node)
  383.  
  384.     
  385.     def ignorableWhitespace(self, chars):
  386.         PullDOM.ignorableWhitespace(self, chars)
  387.         node = self.lastEvent[0][1]
  388.         parentNode = self.elementStack[-1]
  389.         parentNode.appendChild(node)
  390.  
  391.     
  392.     def characters(self, chars):
  393.         PullDOM.characters(self, chars)
  394.         node = self.lastEvent[0][1]
  395.         parentNode = self.elementStack[-1]
  396.         parentNode.appendChild(node)
  397.  
  398.  
  399. default_bufsize = 16364
  400.  
  401. def parse(stream_or_string, parser = None, bufsize = None):
  402.     if bufsize is None:
  403.         bufsize = default_bufsize
  404.     
  405.     if type(stream_or_string) in _StringTypes:
  406.         stream = open(stream_or_string)
  407.     else:
  408.         stream = stream_or_string
  409.     if not parser:
  410.         parser = xml.sax.make_parser()
  411.     
  412.     return DOMEventStream(stream, parser, bufsize)
  413.  
  414.  
  415. def parseString(string, parser = None):
  416.     
  417.     try:
  418.         StringIO = StringIO
  419.         import cStringIO
  420.     except ImportError:
  421.         StringIO = StringIO
  422.         import StringIO
  423.  
  424.     bufsize = len(string)
  425.     buf = StringIO(string)
  426.     if not parser:
  427.         parser = xml.sax.make_parser()
  428.     
  429.     return DOMEventStream(buf, parser, bufsize)
  430.  
  431.